package com.aks.server.service.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.aks.server.domain.response.em.ResultEnum;
import com.aks.server.domain.response.ResponseResult;
import com.aks.server.domain.request.param.AuthLoginParam;
import com.aks.server.entity.UserEntity;
import com.aks.server.mapper.AuthMapper;
import com.aks.server.service.AuthService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;



@Service("authService")
public class AuthServiceImpl extends BaseServiceImpl implements AuthService {
    @Resource
    private AuthMapper authMapper;

    private static final Integer DEFAULT_LOGIN_SESSION_TIMEOUT = 60 * 60 * 24 * 7;

    @Override
    public ResponseResult<?> login(AuthLoginParam param) {
        UserEntity entity=authMapper.selectByLogin(param.getMobile(), param.getLoginPassword());
        if(entity==null){
            return ResponseResult.fail(ResultEnum.AUTH_FAIL);
        }
        StpUtil.login(entity.getId(), new SaLoginModel().setIsLastingCookie(true)
                .setTimeout(DEFAULT_LOGIN_SESSION_TIMEOUT));
        StpUtil.getSession().set(entity.getId().toString(), entity);
        return ResponseResult.success(ResultEnum.AUTH_SUCCESS,entity);
    }
}
